package servlets;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestHandler;

import services.TipoServiceIntf;
import services.UsuarioServiceIntf;

@Component("tipos")
public class Tipos extends HttpServlet implements HttpRequestHandler{

	private static final long serialVersionUID = 1L;
	
	private static int operacion;
	private static String nombre;
	private static Long edad;
	private static Long id;
	
	
	
	@Autowired
	private TipoServiceIntf tipoService;
	

	@SuppressWarnings("unchecked")
	public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1)
			throws ServletException, IOException {
		System.out.println("USUARIOS");
		
		Enumeration<String> parametros = arg0.getParameterNames();
		
		while (parametros.hasMoreElements()) {
			String parametro= (String) parametros.nextElement();
				switch(parametro){
					case "_OPERACION":
							operacion = Integer.parseInt((String) arg0.getParameter(parametro));
						break;
					case "_NOMBRE":
							nombre = (String) arg0.getParameter(parametro);
						break;
					case "_EDAD":
							edad =  new Long((String)arg0.getParameter(parametro));
						break;
					case "_ID":
						id =  new Long((String)arg0.getParameter(parametro));
					break;
					
					default:
						break;
											
				}
		}
		
		ejecutaOperacion(arg1);
		
	}


	private void ejecutaOperacion(HttpServletResponse response) throws IOException {
		
		switch(operacion){
			case 1://ALTA
				try{
					response.getOutputStream().print("ok");
				}catch (Exception e) {
					response.getOutputStream().print("ko");
				}
				
				break;
			case 2://BAJA
				try{
					response.getOutputStream().print("ok");
				}catch (Exception e) {
					response.getOutputStream().print("ko");
				}
				break;
			case 3://LOG IN
				try{
					response.getOutputStream().print("ok");
				}catch (Exception e) {
					response.getOutputStream().print("ko");
				}
				break;
			default:
				break;
		}
		
	}

	


}
